home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2043 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.7 KB  |  98 lines

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: richard@moriarty.com (Richard Jason Lyders)
  3. Newsgroups: comp.sys.mac.oop.misc,comp.sys.mac.programmer.codewarrior,comp.lang.c++,comp.lang.c++.moderated
  4. Subject: [Q] [enums] class_ID as enum is basically useless? Why not GetClassID()?
  5. Date: 14 Jan 1996 20:20:47 -0000
  6. Organization: Moriarty & Associates, Inc.
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: Dietmar.Kuehl@uni-konstanz.de
  9. Message-ID: <4dboiv$rlm@netlab.cs.rpi.edu>
  10. NNTP-Posting-Host: netlab.cs.rpi.edu
  11. X-Original-Date: Sat, 13 Jan 1996 19:53:19 -0600
  12.  
  13. HELP! Suggestions are MOST WELCOME!
  14.  
  15. Why would someone use an enum for a class_ID in a class library? Why not
  16. simply provide a GetClassID() function for each class? That would be so
  17. much simpler and so much more useful!
  18.  
  19. I don't seem to be able to use the enum class_ID  to find out what class
  20. an object is because class_ID isn't "virtual" like a function. If you have
  21. an object of a class that is derived from a base class, and you are
  22. referencing that object using the base pointer, then class_ID does not
  23. tell you the class of the object, it tells you the class of the pointer.
  24. What god is this? I know what class the pointer is because I cast it
  25. myself! Duh?
  26.  
  27. This means that I don't seem to be able to create a routine that uses a
  28. base class as a pointer to any of that base class's derived classes. . .
  29. because in that routine I need to know exactly what the class_id is of the
  30. REAL object rather than the class_ID of the base object (which is pretty
  31. darn obvious because it never changes).
  32.  
  33. I did not expect to run into this problem. Is this a limitation? an error
  34. on my part? Is there another way to find out the actual class of an object
  35. without using a class_ID enum that really will give me the correct
  36. class_ID?
  37.  
  38. So , by using an enum . . .whatever you type cast a pointer as. . . that's
  39. the class ID that you get. . . so it isn't any better than a compiler
  40. #define as far as I am trying to use it.
  41.  
  42. If you check out my code example below. . .you will see my confusion as to
  43. why a class_ID enum would be used . . . and I hope ther is another way of
  44. finding out an object's actual class ID.
  45.  
  46. PS: and no, it is not possible to give each class a GetClassID routine
  47. because I am having this problem with the Metrowerks PowerPlant class
  48. library and I don't think it is very acceptible to  just go around
  49. changing the inner workings of a class library.
  50.  
  51. class LPane {
  52.    public:
  53.       enum { class_ID = 'pane' };
  54.       LPane() {};
  55.       ~LPane() {}; 
  56. };
  57.  
  58. class LView : public LPane {
  59.    public:
  60.       enum { class_ID = 'view' };
  61.       LView() {};
  62.       ~LView() {};
  63. };
  64.  
  65. void main (void) {
  66.  LPane aPane;
  67.  LView aView;
  68.  LPane * aPanePtr = &aPane;
  69.  LView * aViewPtr =  &aView;
  70.  LPane * aPanePtr2 = (LPane*)aViewPtr;
  71.  
  72.  long aPaneClassID = aPanePtr->class_ID;    // = 'pane' (obviously) 
  73.  long aViewClassID = aViewPtr->class_ID;    // = 'view' (obviously)
  74.  long aWhichClassID = aPanePtr2->class_ID;  // = 'pane' ********* why?
  75. **********
  76. }
  77.  
  78. /*                                   
  79.                                      ?
  80.  ___  o     _             _         /
  81. | o \ _ ___| |_ ___ _ _ _| |    /"""\
  82. |   /| | __)   | o | '_| o |    |~'~|       
  83. |_|_\|_\___)_|_|_|_|_| \___|    \ - /     
  84. .---------------------------------^----. 
  85. | Richard Jason Lyders  Houston, Texas |
  86. |         richard@moriarty.com         |
  87. '-----------------------------OOo----oOO
  88.                                 |=[]=|       
  89.                                 /_/\_\            
  90.                                oOO  OOo
  91.  
  92. */
  93.  
  94.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  95.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  96.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  97.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  98.